package com.yunkeji.admin.controller.gift;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yunkeji.common.annotation.Log;
import com.yunkeji.common.annotation.RepeatSubmit;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.core.validate.AddGroup;
import com.yunkeji.common.core.validate.EditGroup;
import com.yunkeji.common.enums.BusinessType;
import com.yunkeji.common.utils.poi.ExcelUtil;
import com.yunkeji.dao.vo.GiftVo;
import com.yunkeji.system.domain.bo.GiftBo;
import com.yunkeji.system.service.IGiftService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 礼物配置信息
 *
 * @author zhang
 * @date 2024-07-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/gift")
public class GiftController extends BaseController {

    private final IGiftService iGiftService;

    /**
     * 禁用/启用
     *
     * @param id 主键
     */
    @SaCheckPermission("system:gift:disable")
    @GetMapping("/disable/{id}")
    public R<Void> disable(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        iGiftService.disable(id);
        return R.ok();
    }

    /**
     * 删除
     *
     * @param id 主键
     */
    @SaCheckPermission("system:gift:del")
    @GetMapping("/del/{id}")
    public R<Void> del(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        iGiftService.del(id);
        return R.ok();
    }

    /** 查询礼物配置信息列表 */
    @SaCheckPermission("system:gift:list")
    @GetMapping("/list")
    public TableDataInfo<GiftVo> list(GiftBo bo, PageQuery pageQuery) {
        return iGiftService.queryPageList(bo, pageQuery);
    }

    /** 导出礼物配置信息列表 */
    @SaCheckPermission("system:gift:export")
    @Log(title = "礼物配置信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(GiftBo bo, HttpServletResponse response) {
        List<GiftVo> list = iGiftService.queryList(bo);
        ExcelUtil.exportExcel(list, "礼物配置信息", GiftVo.class, response);
    }

    /**
     * 获取礼物配置信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:gift:query")
    @GetMapping("/{id}")
    public R<GiftVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iGiftService.queryById(id));
    }

    /** 新增礼物配置信息 */
    @SaCheckPermission("system:gift:add")
    @Log(title = "礼物配置信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GiftBo bo) {
        return toAjax(iGiftService.insertByBo(bo));
    }

    /** 修改礼物配置信息 */
    @SaCheckPermission("system:gift:edit")
    @Log(title = "礼物配置信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GiftBo bo) {
        return toAjax(iGiftService.updateByBo(bo));
    }

    /**
     * 删除礼物配置信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:gift:remove")
    @Log(title = "礼物配置信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iGiftService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
